local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local playerauthdao =  require "playerauthdao"
local md5 = require "md5"
local auth = require "auth"
local timetool = require "timetool"
local configdao = require "configdao"
local processstate = require "processstate" 
require "enum"

local processing = processstate:new({timeout = 4})
local  AgentRegister = {}

--[[
//请求注册
message RegisterReq {
	optional Version version = 1;   
	optional int32 auth_type = 2;	 // 账号类型 1 游客 2 微信 3 
	optional string user_name = 3;	 // 账号名
   	optional string cpass = 4; 		 // 注册时密码 client转换为utf8后md5值
    optional string regmobileid = 5; // 注册设备id
    optional string reglocation = 6; // 玩家注册位置
    optional string option_data = 7; // 可选数据
}

//响应client注册
message RegisterRes {
	optional int32 errcode = 1;    //错误原因 0表示成功
	optional string errcodedes = 2;//错误描述
	optional string user_name = 3; //账号名 client本地记录,下次登陆如果本地已经存在走RegLogin流程,否则从新走注册流程 
	optional string cpass = 4;     //注册密码md5 client本地记录,下次登陆如果本地已经存在走RegLogin流程,否则从新走注册流程
	optional int32 uid = 5;        //账号uid
	optional int32 expiretime = 6; //过期时间戳
	optional string token = 7;  //登陆游戏服务器的认证token
}
]]
function  AgentRegister.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper.get_server()
	
	if processing:is_processing() then
		filelog.sys_warning("AgentRegister.process processing:is_processing")
		return
	end
	processing:set_process_state(true)

	local status, userinfo, cpass
	if request.auth_type == EauthType.AUTH_TYPE_WECHAT then
		status, userinfo = playerauthdao.query_player_uidbyusername(request.user_name)
		cpass = md5.sumhexa("1234567890".."20161115")
	elseif request.auth_type == EauthType.AUTH_TYPE_GUEST then
		status, userinfo = playerauthdao.query_playerfrom_alldbbyregmobileid(request.regmobileid)
		cpass = md5.sumhexa(request.regmobileid.."20161115")
	elseif request.auth_type == EauthType.AUTH_TYPE_SELF then
		status, userinfo = playerauthdao.query_player_uidbyusername(request.user_name)
		cpass = request.cpass
	end

	if not status then
		responsemsg.errcode = EErrCode.ERR_ACCESSDATA_FAILED
		responsemsg.errcodedes = "登陆访问数据失败！"
		msghelper:send_resmsgto_client(fd, "RegisterRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return
	end

	if userinfo == nil then
		--注册账号
		userinfo = {}
		userinfo.cpass = cpass
        userinfo.uid = request.uid
        userinfo.auth_type = request.auth_type

        if request.auth_type == EauthType.AUTH_TYPE_WECHAT then
        	userinfo.user_name = request.user_name
		elseif request.auth_type == EauthType.AUTH_TYPE_GUEST then
        	userinfo.user_name = "_g"..request.uid
        elseif request.auth_type == EauthType.AUTH_TYPE_SELF then
        	userinfo.user_name = request.user_name        	 
		end
 
        userinfo.slat_pass = auth.generate_md5slat_pass(userinfo.cpass) 
        userinfo.regmobileid = request.regmobileid
        userinfo.reglocation = request.reglocation
        userinfo.regip = server.ip
        userinfo.platform = request.version.platform
        userinfo.channel = request.version.channel
       	userinfo.isblock = 0
        userinfo.createtime = timetool.get_time()

        playerauthdao.save_player_userinfo("insert", userinfo.user_name, userinfo)

	elseif request.auth_type == EauthType.AUTH_TYPE_SELF then
		responsemsg.errcode = EErrCode.ERR_AUTH_REPEATED
		responsemsg.errcodedes = "当前账号名已经注册过！"
		msghelper:send_resmsgto_client(fd, "RegisterRes", responsemsg)
		processing:set_process_state(false)
		server:exit_agent()
		return				
	end

	responsemsg.user_name = userinfo.user_name			
	responsemsg.cpass = userinfo.cpass
	responsemsg.uid = userinfo.uid
	local now_time = timetool.get_time()
	responsemsg.expiretime = now_time + configdao.get_common_conf("tokentimeout")

	local signstr = responsemsg.expiretime.."&&"..responsemsg.uid.."&&"..responsemsg.user_name
	responsemsg.token = auth.generate_thirdauth_md5token(signstr)

	msghelper:send_resmsgto_client(fd, "RegisterRes", responsemsg)
	processing:set_process_state(false)
	server:exit_agent()
end

return AgentRegister
